package com.cynteka.cyfoman.ui;

import com.cynteka.cyfoman.core.ValueObject;
import com.cynteka.cyfoman.core.config.ParamMD;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class ParamPanelBuilder implements UIBuilder {

    private ParamWidgetFactory paramWidgetFactory;
    private static final String DEFAULT_GROUP = "_______________default___group_______";

    public ParamPanelBuilder() {
    }

    public ParamWidgetFactory getParamWidgetFactory() {
        return paramWidgetFactory;
    }

    public void setParamWidgetFactory(ParamWidgetFactory paramWidgetFactory) {
        this.paramWidgetFactory = paramWidgetFactory;
    }

    protected String getGroup(ParamMD paramMD) {
        String res = paramMD.getGroup();
        res = res == null ? DEFAULT_GROUP : res;
        return res;
    }

    protected List<String> getGroups(List<ParamMD> params) {
        List<String> res = new ArrayList<String>();
        for (ParamMD param : params) {
            String groupName = getGroup(param);
            if (!res.contains(groupName))
                res.add(groupName);
        }
        return res;
    }

    public void createContentPane(final Container container2, List<ParamMD> params, ValueObject value) {

        List<String> groups = getGroups(params);
        Container container = null;
        JTabbedPane tabPanel = null;
        if (groups.size() > 1) {
            container2.setLayout(new GridLayout(1, 1));
            tabPanel = new JTabbedPane();
            container2.add(tabPanel);
        } else {
            container = container2;
        }


        for (String group : groups) {
            if (groups.size() > 1) {
                container = new JPanel();
                String title = group;
                title = DEFAULT_GROUP.equals(title) ? "Основные параметры" : title;
                tabPanel.addTab(title, container);
            }
            LayoutManager layout = new GridBagLayout();
            container.setLayout(layout);

            GridBagConstraints gbc = new GridBagConstraints();

            int row = 0;
            for (ParamMD param : params) {
                if (getGroup(param).equals(group)) {
                    gbc.fill = GridBagConstraints.HORIZONTAL;
                    gbc.gridy = row;
                    gbc.gridx = 0;
                    gbc.weightx = 0.5;
                    gbc.ipady = 5;
                    gbc.insets = new Insets(5, 5, 5, 5);

                    Component label = paramWidgetFactory.createLabel(param);
                    container.add(label, gbc);

                    if (param.isRequired()) {
                        gbc.fill = GridBagConstraints.HORIZONTAL;
                        gbc.gridy = row;
                        gbc.gridx = 1;
                        gbc.weightx = 0.1;
                        gbc.ipady = 0;
                        gbc.insets = new Insets(0, 0, 0, 0);
                        Component reqLabel = paramWidgetFactory.createRequiredMarker();
                        container.add(reqLabel, gbc);
                    }

                    //gbc = new GridBagConstraints();

                    gbc.fill = GridBagConstraints.HORIZONTAL;
                    gbc.gridx = 2;       //aligned with button 2
                    gbc.gridy = row;       //third row
                    gbc.weightx = 5;
                    gbc.ipady = 5;
                    gbc.insets = new Insets(5, 5, 5, 5);

                    Component editor = paramWidgetFactory.createEditor(param, value);
                    container.add(editor, gbc);

                    row++;
                }
            }
        }
    }
}
